package com.shijianchen.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shijianchen.mapper.SpOtherAdminMapper;
import com.shijianchen.projo.SpPlatform;
import com.shijianchen.projo.SpOtherAdmin;
import com.shijianchen.projo.query.GetUserQuery;
import com.shijianchen.projo.vos.UserItem;
import com.shijianchen.service.SpOtherAuthRuleService;
import com.shijianchen.service.impl.UserServiceImpl;
import com.shijianchen.utils.BaseControllerUtil;
import com.shijianchen.utils.JwtUtil;
import com.shijianchen.utils.Result;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/pro-api")
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private SpOtherAdminMapper spOtherAdminMapper;
    @Autowired
    private SpOtherAuthRuleService spOtherAuthRuleService;
    @PostMapping("/Admin/Login")
    @ResponseBody
    public Result<Object> login(@RequestBody SpOtherAdmin spOtherAdmin) throws IllegalAccessException {
        SpOtherAdmin loginRet = userService.login(spOtherAdmin);
        if (loginRet == null){
            return Result.fail("登录失败，账号或密码错误");
        }
        loginRet.setToken(null);
        String s = JwtUtil.generate(loginRet);
        loginRet.setToken(s);
        return Result.ok(loginRet);
    }
    @GetMapping("auth/getUserInfo")
    public Result<Object> authGetUserInfo(@RequestHeader(value = "Authorization") String token) throws Exception {
        Claims userInfo = BaseControllerUtil.getUserInfo(token);
        if(userInfo == null){
            return Result.fail("token不存在");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userInfo",userInfo);
        map.put("roles",new String[]{"R_SUPER"});
        return Result.ok(map);
    }
    @GetMapping("/App/login/getPlatform")
    public Result<Object> getPlatform(@RequestHeader HttpHeaders headers){
        List<SpPlatform> ret = userService.getPlatform();
        return Result.ok(ret);
    }
    @PostMapping("/App/user/list")
    public Result<Object> getUserList(@RequestBody GetUserQuery query, @RequestHeader(value = "Authorization") String token) throws Exception {
        Claims userInfo = BaseControllerUtil.getUserInfo(token);
        String userId = (String) userInfo.get("userId");
        Page<UserItem> spOtherAdminPage = new Page<>(query.getCurrent(),query.getSize());
        Page<UserItem> userList = userService.getUserList(spOtherAdminPage,query);
        return Result.ok(userList);
    }
}
